package com._520it.crm.web.controller;

import com._520it.crm.domain.TransferBill;
import com._520it.crm.page.AjaxResult;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.TransferBillQueryObject;
import com._520it.crm.service.IClientService;
import com._520it.crm.service.ITransferBillService;
import com._520it.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/transferBill")
public class TransferBillController {
	@Autowired
	private ITransferBillService transferBillService;
	@Autowired
	private IClientService clientService;
	@RequiresPermissions("transferBill:index")
	@PermissionName("客户移交记录列表")
	@RequestMapping("")
	public String index(){
		return "transferBill";
	}
	@RequiresPermissions("transferBill:list")
	@PermissionName("客户移交记录数据")
	@RequestMapping("/list")
	@ResponseBody
	public PageResult list(TransferBillQueryObject qo){
		PageResult result = transferBillService.queryPage(qo);
		return result;
	}
	@RequiresPermissions("transferBill:save")
	@RequestMapping("/save")
	@ResponseBody
	@PermissionName("客户移交记录新增")
	public AjaxResult save(TransferBill transferBill){
		AjaxResult result = null;
		try{
			clientService.updateManager(transferBill.getClient().getId(),transferBill.getReceiver().getId());
			transferBillService.insert(transferBill);
			result = new AjaxResult(true,"保存成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("保存失败,请联系管理员!");
		}
		return result;
	}
	@RequiresPermissions("transferBill:update")
	@PermissionName("客户移交记录更新")
	@RequestMapping("/update")
	@ResponseBody
	public AjaxResult update(TransferBill transferBill){
		AjaxResult result = null;
		try{
			transferBillService.updateByPrimaryKey(transferBill);
			result = new AjaxResult(true,"更新成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("更新失败,请联系管理员!");
		}
		return result;
	}
	@RequiresPermissions("transferBill:delete")
	@PermissionName("客户移交记录删除")
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResult delete(Long transferBillId){
		AjaxResult result = null;
		try{
			transferBillService.deleteByPrimaryKey(transferBillId);
			result = new AjaxResult(true,"删除成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("删除失败,请联系管理员!");
		}
		return result;
	}
}
